# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

	before_filter :fetch_logged_in_user

	protected
	def fetch_logged_in_user
    return unless session[:user_id]
    @current_user = User.find_by_id(session[:user_id])
	end

	def logged_in?
    return !(session[:user_id].nil?)
	end
	helper_method :logged_in?

	def login_required
		return true if logged_in?
		session[:return_to] = request.request_uri
		redirect_to new_session_path and return false
	end

  def post_level_check
    return true if User.get_user_level_id(session[:user_id]) <= Category.post_level_id(params[:cat])
    session[:denied_path] = request.request_uri
    redirect_to denied_path
  end

  def reply_level_check
    return true if User.get_user_level_id(session[:user_id]) <= Category.reply_level_id(params[:parent_id])
    session[:denied_path] = request.request_uri
    redirect_to denied_path
  end

end
